package cn.sibu.plugin.authorize.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import cn.sibu.plugin.authorize.R;

/**
 * 
 *TODO 密码框的回删与密码显示 
 * @data:  2015-4-17 上午9:57:20 
 * @version:  V1.0
 */

public class EditTextWithPwd extends RelativeLayout {
		 
	private Context mContext;
	private EditText editText;
	private ImageView pwd_eys_see;
	private ImageView pwd_del;
	private Editable strEditable;
	public EditTextWithPwd(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		init();
	 }

	public EditTextWithPwd(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		init();
	 }

	public EditTextWithPwd(Context context) {
		super(context);
		mContext = context;
		init();
	}

	 public void init(){
	 LayoutInflater inflater =  LayoutInflater.from(mContext);
	 View view = inflater.inflate(R.layout.edit_pwd_del_see,this);
	 editText = (EditText) view.findViewById(R.id.et_password);
	 pwd_eys_see = (ImageView) view.findViewById(R.id.pwd_eys_see);
	 pwd_del = (ImageView) view.findViewById(R.id.pwd_del);
     editText.addTextChangedListener(new TextWatcher() {
			
		

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				strEditable = s;
				 //设置图片的方法
					setDrawable(s);
				 
		 		
			}
		});
     editText.setOnFocusChangeListener(new OnFocusChangeListener() {
		
		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			 if(hasFocus){
				 if(!TextUtils.isEmpty(strEditable)){
					 pwd_del.setVisibility(View.VISIBLE);
					 pwd_del.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								editText.setText("");
								pwd_del.setVisibility(View.INVISIBLE);
							}
						});
				 }else{
					 pwd_del.setVisibility(View.INVISIBLE);
				 }
				
			 }else{
				 pwd_del.setVisibility(View.INVISIBLE);
			 }
			
		}
	});
	 }

	  
	 @Override
		protected void finalize() throws Throwable {
			super.finalize();
		}
	 
	 private boolean isPwd = true;
	 
	public void setDrawable(Editable s) {
         int len = s.length(); 
		if (len > 0) {

			// 图片可见
			pwd_del.setVisibility(View.VISIBLE);
			// 响应点击事件
			pwd_del.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					editText.setText("");
					pwd_del.setVisibility(View.INVISIBLE);
				}
			});
			//图片可见
			pwd_eys_see.setVisibility(View.VISIBLE);
			pwd_eys_see.setClickable(true);
			pwd_eys_see.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					pwd_del.setVisibility(View.INVISIBLE);
					if (isPwd) {
						editText.setTransformationMethod(HideReturnsTransformationMethod
								.getInstance());
						pwd_eys_see                    
								.setBackgroundResource(R.drawable.grey_eys_open);
						isPwd = false;

					} else {
						pwd_eys_see.setBackgroundResource(R.drawable.grey_eys_close);
						editText.setTransformationMethod(PasswordTransformationMethod
								.getInstance());
						 isPwd = true;
					}

				}
			 });
			//光标位置显示在末尾
			editText.setSelection(len);
		}else{
			//如果没有字符，隐藏图片
			pwd_del.setVisibility(View.INVISIBLE);
			pwd_eys_see.setClickable(false);
			 
		}
	}
	 
	 public   String getPwd(){
		 return editText.getText().toString().trim();
	 }
}
